package com.alanjaver.msgpush;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.widget.Toast;

@SuppressWarnings("deprecation")
public class NotifHandleActivity extends Activity
{
	private static final String	TAG				= "NotifHandleActivity";
	private static final byte	DATATYPE_URL	= 1;
	private static final byte	DATATYPE_TEXT	= 2;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		android.util.Log.w(TAG, "onCreate");

		Intent itt = getIntent();

		handlerIntent(itt);

		finish();
	}

	@Override
	protected void onDestroy()
	{
		android.util.Log.w(TAG, "onDestroy");
		super.onDestroy();
	}

	private void handlerIntent(Intent itt)
	{
		if (itt != null)
		{
			// String name = itt.getStringExtra("name");
			String data = itt.getStringExtra("data");
			String type = itt.getStringExtra("type");
			// long time = itt.getLongExtra("time", 0);

			android.util.Log.w(TAG, "handlerIntent, type : " + type + ", data : " + data);

			switch (transTypeStr(type))
			{
				case DATATYPE_URL:
					Intent intent = new Intent();
					intent.setAction("android.intent.action.VIEW");
					Uri content_uri_browsers = Uri.parse(data);
					intent.setData(content_uri_browsers);
					intent.setClassName("com.android.browser",
							"com.android.browser.BrowserActivity");
					this.startActivity(intent);
					break;
				case DATATYPE_TEXT:
					ClipboardManager clip = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
					clip.setText(data); // 复制

					Toast.makeText(this, "文本已复制。", Toast.LENGTH_LONG).show();
					break;
				default:
					android.util.Log.e(TAG, "Unknow type : " + type);
			}
		}
	}

	private byte transTypeStr(String type)
	{
		if ("url".equals(type))
			return DATATYPE_URL;
		if ("text".equals(type))
			return DATATYPE_TEXT;
		return 0;
	}
}
